<?php


namespace utils;

use Closure;

class JsArray
{
    public static function sort(array $array,Closure $cb): array
    {
        usort($array,$cb);
        return $array;
    }
    public static function filter(array $array,Closure $cb): array
    {
        $return = [];
        foreach($array as $arr)
        {
            if((bool)call_user_func($cb,$arr))
            {
                $return[]=$arr;
            }
        }
        return $return;
    }
    public static function find(array $array,Closure $cb)
    {
        foreach($array as $arr)
        {
            if((bool)call_user_func($cb,$arr))
            {
                 return $arr;
            }
        }
        return null;
    }
    public static function findIndex(array $array,Closure $cb)
    {
        foreach($array as $index=>$arr)
        {
            if((bool)call_user_func($cb,$arr))
            {
                 return $index;
            }
        }
        return -1;
    }
    
}